package com.dfdchem.imd.kaiwu.activity.base;

import android.app.Activity;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;

import com.dfdchem.imd.kaiwu.activity.LoginRequiredBroadcast;
import com.dfdchem.imd.kaiwu.constant.RouterPath;
import com.dfdchem.imd.kaiwu.manager.DataManager;

import zuo.biao.library.util.Log;

/**
 * 框架中础需要登录的 Activity
 */
public abstract class BaseActivity extends BaseActivityAnon{
    private static final String TAG = "BaseActivity";

    private LoginRequiredBroadcast loginRequiredBroadcast;

    @Override
    public Activity getActivity(){
        return this; //必须return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, this.getClass().getSimpleName() + " onCreate");
        super.onCreate(savedInstanceState);

        // 当接口登录授权过期时，发送LOGIN_REQUIRED广播(用于关闭已经启动的Activity)，然后启动LoginActivity
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(LoginRequiredBroadcast.NAME);

        loginRequiredBroadcast = new LoginRequiredBroadcast();
        registerReceiver(loginRequiredBroadcast, intentFilter);
    }

    public void logout(){
        DataManager.getInstance().putXToken(null);
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                toActivity(RouterPath.LOGIN_ACTIVITY);
                finish();
                overridePendingTransition(zuo.biao.library.R.anim.bottom_push_in, 0);
            }
        }, 250);
    }

    @Override
    public abstract void initView();

    @Override
    public abstract void initData();

    @Override
    public abstract void initEvent();

    @Override
    protected void onDestroy(){
        unregisterReceiver(loginRequiredBroadcast);
        super.onDestroy();
    }

}
